package com.example.yd202xslzy.recyclerview;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import com.example.yd202xslzy.R;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter <ItemViewHolder>{
    private List<DataItem> dataItemList;

    //提示框监听器
    OnItemClickListener mOnItemClickListener;
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        mOnItemClickListener=listener;
    }



    public void setDate(List dataItemList){
        this.dataItemList=dataItemList;
        notifyDataSetChanged();//通知数据集发生变化
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        //获取Layoutlnflater对象，他可以根据布局定义的内容生成一个View对象
        LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
        //生成View对象
        View itemView = layoutInflater.inflate(R.layout.item_layout,viewGroup,false);
        //实例化一个ViewHolder
        ItemViewHolder viewHolder = new ItemViewHolder(itemView);
        //返回ViewHolder
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder itemViewHolder, final int i) {
        final DataItem dateItem = dataItemList.get(i);
        itemViewHolder.textView1.setText(dateItem.getText1());
        itemViewHolder.textView2.setText(dateItem.getText2());


        //提示框监听器
        if(mOnItemClickListener!=null)
        {
            itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //System.out.println("dawangba:"+dataItemList.get(i).getText1());
                    //Toast.makeText(view.getContext(),"404"+dataItemList.get(i).getText1(), Toast.LENGTH_SHORT).show();
                    mOnItemClickListener.onItemClick(view,i);
                }
            });
        }




    }

    @Override
    public int getItemCount() {

        return dataItemList.size();//返回列表显示的数据个数
    }
}
